package com.zws.cucumber.optimization.socket;

import com.zws.cucumber.optimization.ZwsOptimizations;
import com.zws.cucumber.optimization.socket.oio.ZwsSocketImplFactory;

import java.io.IOException;
import java.net.Socket;

public class ZwsSockets {

    private static Thread mainThread;

    private static volatile boolean isInit = false;

    public static void init() throws IOException {
        if (!isInit) {
            synchronized (ZwsSockets.class) {
                if (!isInit) {
                    isInit = true;
                    mainThread = Thread.currentThread();
                    if (ZwsOptimizations.isSocketMemorySocketEnabled()) {
                        Socket.setSocketImplFactory(new ZwsSocketImplFactory());
                    }
                }
            }
        }
    }

    public static boolean isInit() {
        return isInit;
    }

    public static boolean isMainThread() {
        return mainThread == Thread.currentThread();
    }
}
